# -*- coding: utf-8 -*-
#coding=utf-8

import web
import os
import config
from action.admin.base import adminAction
from model.company import company
from model.companycomment import companycomment as comment
from model.category import category
from model.album import album
from model.image import image

'''
Admin controller: producing company comment administration views.
'''
class refererAction(adminAction):
    def __init__(self, name = '评论管理'):
        adminAction.__init__(self, name)


    def GET(self, name):
        if name == 'companylist':
            return self.companylist()
        if name == 'list':
            return self.list()
        elif name == 'delete':
            return self.delete()
        elif name == 'edit':
            return self.edit()
        else:
            return self.notFound()

    def POST(self, name):
        if name == 'modify':
            return self.modify()
        elif name == 'deleteBatch':
            return self.deleteBatch()
        else:
            return self.notFound()

    def list(self):
        inputParams = self.getInput()
        page = int(inputParams['page']) if inputParams.has_key('page') else 1
        count = config.COUNT_PER_PAGE
        offset= (page-1)*count if page > 0 else 0
   
        condition = {}
        commentObj = comment()
        listData = commentObj.getOne('COUNT(*) AS `total`', condition)
        totalCount = listData['total']
        commentList = commentObj.getList('*', condition, 'id desc', str(offset) + ',' + str(count))

        pageString = self.getPageStr(self.makeUrl('/admin/companycomment/list'), page, count, totalCount)
        self.privData['COMMENT_LIST'] = commentList
        self.privData['PAGE_STRING'] = pageString

        return self.display('companycommentList')

    def companylist(self):
        inputParams = self.getInput()
        page = int(inputParams['page']) if inputParams.has_key('page') else 1
        count = config.COUNT_PER_PAGE
        offset= (page-1)*count if page > 0 else 0
   
        condition = {}
        companyObj = company()
        listData = companyObj.getOne('COUNT(*) AS `total`', condition)
        totalCount = listData['total']
        companyList = companyObj.getList('*', condition, 'id desc', str(offset) + ',' + str(count))

        pageString = self.getPageStr(self.makeUrl('/admin/companycomment/companylist'), page, count, totalCount)
        self.privData['COMPANY_LIST'] = companyList
        self.privData['PAGE_STRING'] = pageString

        categoryList = category().getList('*', condition)
        self.privData['CATEGORY_LIST'] = {}
        for i in categoryList:    
            self.privData['CATEGORY_LIST'][i['id']] = i['name']
        return self.display('companycommViewList')

    def delete(self):
        inputParams = self.getInput()
        try:
            condition = {'id': int(inputParams['id'])}
            comment().delete(condition)
        except Exception, e:
            return self.error(msg='评论删除失败: %s' % e, url=self.makeUrl('/admin/companycomment/list'))

        return self.success(msg='评论删除成功', url=self.makeUrl('/admin/companycomment/list'))

    def deleteBatch(self):
        inputParams = self.getInput()
        try:
            condition = ' id IN (' + inputParams['delitems'] +')'
            comment().delete(condition)
        except Exception, e:
            return self.error(msg='评论删除失败: %s' % e, url=self.makeUrl('/admin/companycomment/list'))

        return self.success(msg='评论删除成功', url=self.makeUrl('/admin/companycomment/list'))

    def modify(self):
        userInput= self.getInput()
        try: 
            data={
                'content':      userInput['content'],
            }

            condition={'id': int(userInput['id'])}
            comment().update(data, condition)
        except Exception, e:
            return self.error(msg = '评论修改失败: %s' % e, url=self.makeUrl('/admin/companycomment/list'))

        return self.success('评论修改成功', url=self.makeUrl('/admin/companycomment/list'))

    def edit(self):
        inputParams = self.getInput()
        condition={'id': int(inputParams['id'])}
        commentObj = comment().getOne('*', condition)
        if not commentObj:
            return self.error(msg = '评论不存在')
        self.privData['COMMENT_INFO'] = commentObj

        return self.display('companycommentEdit')

